package com.engine.budget.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class GetYszxdbCmd extends AbstractCommonCommand<Map<String,Object>> {

    public GetYszxdbCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }


    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        Map<String, Object> map = new HashMap<>();

        RecordSet rs = new RecordSet();
        //4.定义SQL语句
        String sql="select id,ysnd,dwjzdgz,ysxm,yskm,yssx,dh,rq,ndys,dzysnz,dzyslx,kyysze,zyys,zhys,fsze,ysye from uf_yszxdb where 1 = 1";
        String sqlTotal="select count(1) total from uf_yszxdb where 1 = 1";


        ArrayList<String> sqlArrs = new ArrayList<>();
        ArrayList<String> sqlTotalArrs = new ArrayList<>();
        String budgetPeriod = (String)params.get("budgetPeriod");
        if(budgetPeriod != null&&!budgetPeriod.isEmpty()){
            sql+=("AND ysnd = ? ");
            sqlTotal+=("AND ysnd = ? ");
            sqlArrs.add(budgetPeriod);
            sqlTotalArrs.add(budgetPeriod);
        }

        String budgetItem = (String)params.get("budgetItem");
        if(budgetItem!=null&&!budgetItem.isEmpty()){
            sql+=("AND ysxm = ? ");
            sqlTotal+=("AND ysxm = ? ");
            sqlArrs.add(budgetItem);
            sqlTotalArrs.add(budgetItem);
        }

        String budgetAccount = (String)params.get("budgetAccount");
        if(budgetAccount!=null&&budgetAccount!=""){
            sql+=("AND yskm = ? ");
            sqlTotal+=("AND yskm = ? ");
            sqlArrs.add(budgetAccount);
            sqlTotalArrs.add(budgetAccount);
        }

        String event = (String)params.get("event");
        if(event!=null&&event!=""){
            sql+=("AND yssx = ? ");
            sqlTotal+=("AND yssx = ? ");
            sqlArrs.add(event);
            sqlTotalArrs.add(event);
        }

        String exeUnit = (String)params.get("exeUnit");
        if(exeUnit!=null&&exeUnit!=""){
            sql+=("AND zhdw = ? ");
            sqlTotal+=("AND zhdw = ? ");
            sqlArrs.add(exeUnit);
            sqlTotalArrs.add(exeUnit);
        }

        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sqlTotal,sqlTotalArrs);
        while (recordSet.next()) {
            map.put("total",recordSet.getString("total"));
        }


        int pageIndex = Util.getIntValue(params.get("pageIndex").toString(), 0);
        int pageSize = Util.getIntValue(params.get("pageSize").toString(), 0);
        if(pageIndex!=0&&pageSize!=0){
            sql+="LIMIT "+pageSize+" OFFSET "+((pageIndex-1)*pageSize);
        }else {
            sql+=("LIMIT 10 OFFSET 0 ");
        }


        rs.executeQuery(sql,sqlArrs);
        //rs.executeUpdate();
        ArrayList<Map> list = new ArrayList<>();
        //7.处理结果，并获取数据
        while (rs.next()){
            HashMap<String, Object> data = new HashMap<>();
            data.put("ysnd",rs.getString("ysnd"));
            data.put("bmgz",rs.getString("dwjzdgz"));
            data.put("ysxm",rs.getString("ysxm"));
            data.put("yskm",rs.getString("yskm"));
            data.put("yssx",rs.getString("yssx"));
            data.put("zhdw",rs.getString("zhdw"));
            data.put("dh",rs.getString("dh"));
            data.put("rq",rs.getString("rq"));
            data.put("ndys",rs.getString("ndys"));
            data.put("tzysnz",rs.getString("dzysnz"));
            data.put("tzyslx",rs.getString("dzyslx"));
            data.put("kyysze",rs.getString("kyysze"));
            data.put("zyys",rs.getString("zyys"));
            data.put("zxys",rs.getString("zhys"));
            data.put("fsze",rs.getString("fsze"));
            data.put("ysye",rs.getString("ysye"));
            list.add(data);
        }
        map.put("data",list);
        return map;
    }
}
